package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.vo.WxUserIndexVo;
import com.cskaoyan.bean.vo.WxUserOrderVo;
import com.cskaoyan.mapper.MarketOrderMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;

/**
 * @Description:TODO
 * @author: landz
 * @create: 2023-01-01 21:21
 * @version:
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Resource
    MarketOrderMapper marketOrderMapper;

    @Override
    public WxUserOrderVo userIndex() {
        //反射获取属性名
        Field[] fields = WxUserIndexVo.class.getDeclaredFields();
        Integer status;
        WxUserIndexVo wxUserIndexVo = new WxUserIndexVo();
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId = marketUser.getId();
        for (Field field : fields) {
            if ("unship".equals(field.getName())) {
                status = 201;
                wxUserIndexVo.setUnship(marketOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
            if ("unrecv".equals(field.getName())) {
                status = 301;
                wxUserIndexVo.setUnrecv(marketOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
            if ("unpaid".equals(field.getName())) {
                status = 101;
                wxUserIndexVo.setUnpaid(marketOrderMapper.selectOrderStatusByUserId(userId, field.getName(), status));
            }
        }
        //通过IN查询 获取comment为0的订单数
        wxUserIndexVo.setUncomment(marketOrderMapper.selectOrderGoodsByUserId(userId));

        return new WxUserOrderVo(wxUserIndexVo);
    }
}
